<?php

/*
 * Manage login or find matches on facebook app
 */

class Facebook_appController extends Controller {

    public $layout = 'facebookapp';

    public function actionIndex() {
	if (user()->id) {
	    //check user have logged in with facebook
	    $modelSocial = UserOAuth::model()->find('user_id = ' . user()->id . ' and LOWER(provider) = "facebook"');
	    if ($modelSocial) {
		$this->redirect(url('facebook_app/find_match'));
	    }
	}
	$this->render('index');
    }

    public function actionFind_match() {
	$error = array();
	$model = new User();
	if (isset($_GET['search'])) {

	    $criteria = new CDbCriteria();
	    //search age
	    if (isset($_GET['User']['ageFrom']) && $_GET['User']['ageFrom'] && isset($_GET['User']['ageTo']) && $_GET['User']['ageTo']) {
		if ($_GET['User']['ageFrom'] < $_GET['User']['ageTo']) {

		    $criteria->addCondition('t.age BETWEEN ' . $_GET['User']['ageFrom'] . ' AND ' . $_GET['User']['ageTo']);
		} else {
//		    $error[] = "'Age to' can not be less than 'Age from'";
		}
	    }
	    //search height
	    if (isset($_GET['User']['heightFrom']) && $_GET['User']['heightFrom'] && isset($_GET['User']['heightTo']) && $_GET['User']['heightTo']) {
		if ($_GET['User']['heightFrom'] < $_GET['User']['heightTo']) {
		    $criteria->addCondition('t.height BETWEEN ' . $_GET['User']['heightFrom'] . ' AND ' . $_GET['User']['heightTo']);
		} else {
//		    $error[] = "'Height to' can not be less than 'Height from'";
		}
	    }
	    //search location
	    if (isset($_GET['User']['postcode']) && $_GET['User']['postcode']) {
		$criteria->addCondition('t.postcode = ' . $_GET['User']['postcode']);
	    }
	    //search marital
	    if (isset($_GET['User']['marital'])) {
		$criteria->addCondition('t.marital = ' . $_GET['User']['marital']);
	    }
	    //search ethnicity
	    if (isset($_GET['User']['ethnicity'])) {
		$criteria->addCondition('t.ethnicity = ' . $_GET['User']['ethnicity']);
	    }
	    //search Relationship
	    if (isset($_GET['User']['relationship'])) {
		$criteria->addCondition('t.relationship = ' . $_GET['User']['relationship']);
	    }
	    //search zodiac
	    if (isset($_GET['User']['zodiac'])) {
		$criteria->addCondition('t.zodiac = ' . $_GET['User']['zodiac']);
	    }
	    //search smoking
	    if (isset($_GET['User']['smoking'])) {
		$criteria->addCondition('t.smoking = ' . $_GET['User']['smoking']);
	    }
	    //search build
	    if (isset($_GET['User']['build'])) {
		$criteria->addCondition('t.build = ' . $_GET['User']['build']);
	    }
	    //search Children
	    if (isset($_GET['User']['children'])) {
		$criteria->addCondition('t.children = ' . $_GET['User']['children']);
	    }
	    //search religion
	    if (isset($_GET['User']['religion'])) {
		$criteria->addCondition('t.religion = ' . $_GET['User']['religion']);
	    }
	    $criteria->addCondition('t.type != "admin"');
	    $criteria->addCondition('t.user_id != ' . user()->id);
	    $criteria->order = 't.user_id DESC';

	    $iTotalRecord = $model->count($criteria);

	    $page = new CPagination($iTotalRecord);
	    $page->pageSize = User::TOTAL_RECORD;
	    $page->applyLimit($criteria);
	    $this->render('find_match', array(
		'model' => $model,
		'aUsers' => $model->findAll($criteria),
		'pages' => $page,
		'iTotalRecord' => $iTotalRecord,
		'error' => $error
	    ));
	} else {
	    $this->render('find_match');
	}
    }

    public function actionView_user($id) {
	$checkList = 0;
	$model = User::model()->findByPk($id);
	//get user who self flirted
	$listFlirt = Flirt::getList(user()->id);
	//check whether self flirted	
	if (array_search($id, $listFlirt) !== FALSE) {
	    $checkList = 1;
	}
	//check wheter user connected social
	$modelSocial = UserOAuth::model()->find('user_id = ' . user()->id . ' and LOWER(provider) = "facebook"');

	$this->render('view_user', array(
	    'model' => $model,
	    'listFlirt' => $listFlirt,
	    'checkList' => $checkList,
	    'modelSocial' => $modelSocial
	));
    }

}
